#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/09/30
# @Author   :Changshu
# @File     :practice_203.py
# 203. 移除链表元素: 删除链表中等于给定值 val 的所有节点。

class ListNode:
	def __init__(self,x):
		self.val=x
		self.next=None

def removeElements(head: ListNode, val: int) -> ListNode:
	if head is None:
		return None
	p=head
	while p and p.val==val:
		p=p.next
	if p is None or p.next is None:
		return p
	r=p
	q=p.next
	while q:
		if int(q.val)==val:
			p.next=q.next
			p=q
		else:
			p=p.next
		q=q.next
	return r

def printListNode(head:ListNode):
	p=head
	while p:
		if p.next!=None:
			print(p.val,end='->')
		else:
			print(p.val)
		p=p.next

if __name__ == '__main__':
	node1=ListNode('1')
	node2 = ListNode('2')
	node3 = ListNode('6')
	node4 = ListNode('3')
	node5 = ListNode('4')
	node6 = ListNode('5')
	node7 = ListNode('6')
	# node1 = ListNode('5')
	node1.next=node2
	node2.next = node3
	node3.next = node4
	node4.next = node5
	node5.next = node6
	node6.next = node7
	# node1.next = node2
	# printListNode(node1)
	r=removeElements(node1,6)
	printListNode(r)